using CoreProject.DTO;
using CoreProject.IService;
using CoreProject.Model;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreProject.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    [Authorize]
    public class UsersController : ControllerBase
    {
        private readonly IUsersService _service;
        private readonly IMapper _mapper;


        public UsersController(IUsersService service, IMapper mapper)
        {
            _service = service;
            _mapper = mapper;
            
        }

        // GET: api/<UsersController>
        [HttpGet]
        public async Task<ResultData<List<UserDto>>> Get()
        {
            ResultData<List<UserDto>> result = new ResultData<List<UserDto>>();

            var data = await _service.GetUsers();
            var list = _mapper.Map<List<UserDto>>(data);
            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.data = list;
            return result;
        }


        [HttpGet("MonthStatistics")]
        public async Task<ResultData<List<MonthStatisticsDto>>> GetMonthStatistics(int starMonth, int endMonth)
        {
            ResultData<List<MonthStatisticsDto>> result = new ResultData<List<MonthStatisticsDto>>();

            var list = await _service.GetMonthStatistics(starMonth, endMonth);

            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.data = list;
            return result;
        }

        // GET api/<UsersController>/5
        [HttpGet("{id}")]
        public async Task<ResultData<UserDto>> Get(int id)
        {
            var user = await _service.GetUser(id);
            UserDto userDto = _mapper.Map<UserDto>(user);
            ResultData<UserDto> result = new ResultData<UserDto>();
            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.data = userDto;
            return result;
        }


        [HttpGet("list")]
        public async Task<ResultPageList<UserDto>> GetListByPage(int page, int size, string name = "")
        {

            var data = await _service.GetUsersByPage(page, size, name);

            //手动变自动
            var list = _mapper.Map<List<UserDto>>(data.list);

            ResultPageList<UserDto> result = new ResultPageList<UserDto>();
            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.page = page;
            result.size = size;
            result.count = data.count;
            result.data = list;
            return result;
        }

        [HttpGet("vippage")]
        public async Task<ResultPageList<UserDto>> Getvip(int page, int size, string name = "")
        {

            var data = await _service.VipList(name, page,size);

            //手动变自动
            var list = _mapper.Map<List<UserDto>>(data.list);

            ResultPageList<UserDto> result = new ResultPageList<UserDto>();
            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.page = page;
            result.size = size;
            result.count = data.count;
            result.data = list;
            return result;
        }

        // POST api/<UsersController>
        [HttpPost]
        public async Task<ResultHandle> Post(UserWriteDto value)
        {
            User user = _mapper.Map<User>(value);
            user.LoginPwd = MD5Helper.Md5(value.LoginPwd, "X2");
            user.LastTime = DateTime.Now;

            var bol = await _service.AddAsync(user);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "添加成功!" : "添加失败!";
            return result;
        }

        // PUT api/<UsersController>/5
        [HttpPut]
        public async Task<ResultHandle> Put(UserWriteDto value)
        {
            User user = _mapper.Map<User>(value);
            user.LoginPwd = MD5Helper.Md5(value.LoginPwd, "X2");
            user.LastTime = DateTime.Now;

            var bol = await _service.UpdateAsync(user);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "修改成功!" : "修改失败!";
            return result;
        }

        // DELETE api/<UsersController>/5
        [HttpDelete("{id}")]
        public async Task<ResultHandle> Delete(int id)
        {
            User user = new User();
            user.UserId = id;

            var bol = await _service.DeleteAsync(user);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }

        // DELETE api/<UsersController>/5
        [HttpDelete("DeleteBatch")]
        public async Task<ResultHandle> DeleteBatch([FromBody] int[] ids)
        {
            User[] users = new User[ids.Length];
            for (int i = 0; i < ids.Length; i++)
            {
                users[i] = new User() { UserId = ids[i] };
            }

            var bol = await _service.DeleteRangeAsync(users);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }
    }
}
